package com.snbc.string;

import java.util.Random;

public class Demo3 {
    public static void main(String[] args) {
        String str = random(10);
        int num = 30000;
        String str1 = "";
        long startTime = System.currentTimeMillis(); // 开始时间
        System.out.println(startTime);
        for (int i = 1; i <= num; i++) {
            str1 += str;
        }
        long endTime = System.currentTimeMillis(); // 结束时间
        System.out.println(endTime);
        System.out.println(endTime - startTime);
        // StringBuffer连接
        System.out.println("========================================================");
        StringBuffer sb = new StringBuffer();
        long startTime1 = System.currentTimeMillis(); // 开始时间
        System.out.println(startTime1);
        for (int i = 1; i <= num; i++) {
            sb.append(str);
        }
        long endTime1 = System.currentTimeMillis(); // 结束时间
        System.out.println(endTime1);
        System.out.println(endTime1 - startTime1);

    }

    private static String random(int length) {

        char[] array = new char[length];
        int min = '0';
        int max = 'z' + 1;

        for (int i = 0; i < array.length; i++) {
            while (true) {
                char mark = (char) ((Math.random() * (max - min) + min));
                if (Character.isDigit(mark) || Character.isLetter(mark)) {
                    array[i] = mark;
                    break;
                }
            }
        }
        String result = new String(array);
        return result;

    }
}
